package net.fdukedom.epicurus.pm.ws;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import net.fdukedom.epicurus.service.interaction.XMLWrapper;

import org.jdom.Element;
import org.jdom.Attribute;

public class MessageXMLWrapper extends XMLWrapper {
	/**
	 * Message element
	 */
	static final public String MESSAGE = "Message";

	/**
	 * Adressee attribute
	 */
	static final private String ADRESSEE = "Adressee";

	/**
	 * Title attribute
	 */
	static final private String TITLE = "Title";

	/**
	 * IsReaded attribute
	 */
	static final private String ISREADED = "IsReaded";

	/**
	 * Date attribute
	 */
	static final private String DATE = "Date";
	/**
	 * Constructor
	 * 
	 * @param element
	 *            message element
	 */
	public MessageXMLWrapper(Element element) {
		super(element);
	}

	public MessageXMLWrapper(String addressee, String title, String body,
			Date created, boolean isReaded) {
		Attribute attr;
		List<Attribute> attrs;

		element = new Element(MESSAGE);
		element.setText(body);
		attrs = element.getAttributes();

		attr = new Attribute(ADRESSEE, addressee);
		attrs.add(attr);

		attr = new Attribute(TITLE, title);
		attrs.add(attr);
		
		attr = new Attribute(ISREADED, new Boolean(isReaded).toString());
		attrs.add(attr);

		attr = new Attribute(DATE, DateFormat.getDateTimeInstance().format(created));
		attrs.add(attr);
	}

	public String getAdressee() {
		return element.getAttributeValue(ADRESSEE);
	}
	
	public String getTitle() {
		return element.getAttributeValue(TITLE);
	}
	
	public Date getCreated() throws ParseException {
		return DateFormat.getDateTimeInstance().parse(element.getAttributeValue(DATE));
	}
	
	public String getBody() {
		return element.getText();
	}
	
	public boolean isReaded() {
		return Boolean.parseBoolean(element.getAttributeValue(ISREADED));
	}
}
